package com.hw.one.dam2.controller;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.hw.one.dam2.util.UserSiteUtil;
import org.apache.poi.ss.usermodel.Workbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.alibaba.fastjson.JSONArray;
import com.hw.one.core.controller.BaseController;
import com.hw.one.core.result.MsgResult;
import com.hw.one.dam2.common.DamConstant;
import com.hw.one.dam2.common.DataCalculateTypeEnum;
import com.hw.one.dam2.common.DataTypeEnum;
import com.hw.one.dam2.common.ScopeTypeEnum;
import com.hw.one.dam2.common.SiteTypeEnum;
import com.hw.one.dam2.common.SortScopeTypeEnum;
import com.hw.one.dam2.entity.AQIEntity;
import com.hw.one.dam2.entity.ContrastRatioEntity;
import com.hw.one.dam2.entity.IAQIEntity;
import com.hw.one.dam2.service.ICityAirService;
import com.hw.one.dam2.service.IGPointAirService;
import com.hw.one.dam2.service.ISmallAirService;
import com.hw.one.dam2.util.CalendarUtil;
import com.hw.one.dam2.util.ExcelUtil;
import com.hw.one.dam2.util.ResponseUtil;
import com.hw.one.ext.util.UserUtil;
import com.hw.one.util.DateTimeUtil;

/**
 * <p>
 * 标准站数据控制层类<br>
 * </p>
 */
@RestController
@RequestMapping(value = "standardSiteData")
public class StandardSiteDataController extends BaseController {

    @Autowired
    private ICityAirService cityAirService;

    @Autowired
    private IGPointAirService gPointAirService;

    @Autowired
    private ISmallAirService smallAirService;


    /**
     * 获取站点的 空气质量数据
     *
     * @param model
     * @param request
     * @return
     */
    @RequestMapping(value = "findAirQuarlityData", method = RequestMethod.GET)
    public MsgResult findAirQuarlityData(Model model, HttpServletRequest request) {
        // 点位数据：国控点 nationalSite;省控点 provinceSite；小型站 smallAirSite
        String pointType = request.getParameter("pointType");
        // 点位类型 默认 国控点 SiteTypeEnum.NATIONAL_SITE.getValue()
        if (null == pointType || "".equals(pointType)) {
            // return MsgResult.infoResult("点位类型pointType的值为空，查询失败。");
            pointType = SiteTypeEnum.NATIONAL_SITE.getValue();
        }
        // 数据类型：小时数据 hourData；日数据 dayData；5分钟数据 minuiteData
        String dataType = request.getParameter("dataType");
        // 数据类型 默认 小时数据 DataTypeEnum.HOUR_DATA.getValue()
        if (null == dataType || "".equals(dataType)) {
            // return MsgResult.infoResult("数据类型dataType的值为空，查询失败。");
            dataType = DataTypeEnum.HOUR_DATA.getValue();
        }

        // 数据计算类型：实时 realTimeData，累计 addUpTimeData
        String dataCalculateType = request.getParameter("dataCalculateType");
        // 数据类型 默认 实时 DataCalculateTypeEnum.REAL_TIME_DATA.getValue()
        if (null == dataCalculateType || "".equals(dataCalculateType)) {
            // return MsgResult.infoResult("数据计算类型dataCalculateType的值为空，查询失败。");
            dataCalculateType = DataCalculateTypeEnum.REAL_TIME_DATA.getValue();
        }

        // 开始时间
        String startTime = request.getParameter("startTime");
        if (null == startTime || "".equals(startTime)) {
            return MsgResult.infoResult("开始时间startTime的值为空，查询失败。");
        }

        // 结束时间
        String endTime = request.getParameter("endTime");
        if (null == endTime || "".equals(endTime)) {
            return MsgResult.infoResult("开始时间endTime的值为空，查询失败。");
        }
        // 站点编码
        String pointCodes = request.getParameter("pointCodes");

        Map<String, Object> paraMap = new HashMap<String, Object>();
        paraMap.put("pointType", pointType);
        paraMap.put("dataType", dataType);
        paraMap.put("dataCalculateType", dataCalculateType);
        paraMap.put("startTime", startTime);
        paraMap.put("endTime", endTime);
        paraMap.put("pointCodes", pointCodes);
        try {

            List<AQIEntity> aqiList = null;

            // 国控点 或者 省控点 或者 小型站; 判断用户是否具有访问权限
            if ((SiteTypeEnum.NATIONAL_SITE.getValue().equals(pointType) && UserSiteUtil.userIsNotContainSiteTypes("001"))
                || (SiteTypeEnum.PROVINCE_SITE.getValue().equals(pointType) && UserSiteUtil.userIsNotContainSiteTypes("002"))
                || (SiteTypeEnum.SMALL_AIR_SITE.getValue().equals(pointType) && UserSiteUtil.userIsNotContainSiteTypes("004"))) {
                aqiList = gPointAirService.findAirSiteAQIByTime(paraMap);
            } else {
                aqiList = new ArrayList<AQIEntity>();
            }
            // 返回
            return MsgResult.okResult(aqiList);// standardSiteMongoDataService.findStandardSiteAirQuality(request);
        } catch (Exception e) {
            return MsgResult.exceptionResult();
        }
    }

    /**
     * 获取站点的 日报数据
     *
     * @param model
     * @param request
     * @return
     */
    @SuppressWarnings("unchecked")
    @RequestMapping(value = "findAirQualityDayReportData", method = RequestMethod.GET)
    public MsgResult findAirQualityDayReportData(Model model, HttpServletRequest request) {
        // 点位数据：国控点 nationalSite;省控点 provinceSite；小型站 smallAirSite
        String pointType = request.getParameter("pointType");
        // 点位类型 默认 国控点 SiteTypeEnum.NATIONAL_SITE.getValue()
        if (null == pointType || "".equals(pointType)) {
            // return MsgResult.infoResult("点位类型pointType的值为空，查询失败。");
            pointType = SiteTypeEnum.NATIONAL_SITE.getValue();
        }
        Calendar calendar = Calendar.getInstance();
        calendar.add(Calendar.DAY_OF_MONTH, -1);
        // 开始时间
        String startTime = request.getParameter("startTime");
        startTime = null == startTime || "".equals(startTime)
            ? DateTimeUtil.getFormatDateStr(calendar.getTime(), DamConstant.ALL_SDF_YMD) : startTime;
        // 结束时间
        String endTime = request.getParameter("endTime");
        calendar.add(Calendar.DAY_OF_MONTH, 1);
        endTime = null == endTime || "".equals(endTime)
            ? DateTimeUtil.getFormatDateStr(calendar.getTime(), DamConstant.ALL_SDF_YMD) : endTime;

        // 查询条件
        Map<String, Object> paraMap = new HashMap<String, Object>();
        paraMap.put("pointType", pointType);
        paraMap.put("startTime", startTime);
        paraMap.put("pointCodes", request.getParameter("pointCodes"));
        paraMap.put("endTime", endTime);
        // 数据类型，查询日数据
        paraMap.put("dataType", "dayData");
        // 数据计算类型，实时数据
        paraMap.put("dataCalculateType", "realTimeData");
        try {
            List<AQIEntity> aqiList = null;
            // 国控点 或者 省控点 或者 小型站; 判断用户是否具有访问权限
            if ((SiteTypeEnum.NATIONAL_SITE.getValue().equals(pointType) && UserSiteUtil.userIsNotContainSiteTypes("001"))
                || (SiteTypeEnum.PROVINCE_SITE.getValue().equals(pointType) && UserSiteUtil.userIsNotContainSiteTypes("002"))
                || (SiteTypeEnum.SMALL_AIR_SITE.getValue().equals(pointType) && UserSiteUtil.userIsNotContainSiteTypes("004"))) {
                aqiList = gPointAirService.findAirSiteAQIByTime(paraMap);
                // 无效类型
            } else {
                aqiList = new ArrayList<AQIEntity>();
            }
            Map<String, Object> region = UserUtil.getRegion();
            String regionName = (String) region.get("regionName");
            Map<String, Object> returnMap = new HashMap<String, Object>();
            returnMap.put("regionName", regionName);
            returnMap.put("dataList", aqiList);
            // 返回
            return MsgResult.okResult(returnMap);
        } catch (Exception e) {
            e.printStackTrace();
            return MsgResult.exceptionResult();
        }
    }

    /**
     * 获取区县的 综合指数数据
     *
     * @param model
     * @param request
     * @return
     */
    @RequestMapping(value = "findAreaComprehensiveIndexData", method = RequestMethod.GET)
    public MsgResult findAreaComprehensiveIndexData(Model model, HttpServletRequest request) {
        // 排名范围类型: 全国排名 nationalSort;省内排名 provinceSort;区县排名 areaCountySort;办事处排名;
        // 新增： seventyFourCitySort;74城市排名; twentySixCitySort;2+26城市排名;
        // officeSort
        String sortScopeType = request.getParameter("sortScopeType");
        if (null == sortScopeType || "".equals(sortScopeType)) {
            return MsgResult.infoResult("查询条件排名类型sortScopeType的值为空，查询失败。");
        }

        // 数据类型 时报hourData；日报dayData；月报monthData；年报yearData
        // DataTypeEnum.HOUR_DATA.getValue()
        String dataType = request.getParameter("dataType");
        if (null == dataType || "".equals(dataType)) {
            return MsgResult.infoResult("查询条件数据类型dataType为空，查询失败。");
        }

        // 区域范围类型：全部allScope；市区 cityScope；郊县 outskirtsScope
        String scopeType = request.getParameter("scopeType");
        if (null == scopeType || "".equals(scopeType)) {
            // return MsgResult.infoResult("查询条件区域范围类型scopeType为空，查询失败。");
            scopeType = ScopeTypeEnum.ALL_SCOPE.getValue();
        }

        String startTime = request.getParameter("startTime");
        if (null == startTime || "".equals(startTime)) {
            return MsgResult.infoResult("查询条件开始时间startTime为空，查询失败。");
        }
        String endTime = request.getParameter("endTime");
        if (null == endTime || "".equals(endTime)) {
            return MsgResult.infoResult("查询条件结束时间endTime为空，查询失败。");
        }

        // 封装request中的查询条件
        Map<String, Object> paraMap = new HashMap<String, Object>();
        paraMap.put("sortScopeType", sortScopeType);
        paraMap.put("dataType", dataType);
        paraMap.put("scopeType", scopeType);
        paraMap.put("startTime", startTime);
        paraMap.put("endTime", endTime);
        try {
            List<IAQIEntity> iaqiList = null;
            // 全国、省内、区县排名，都在一个表中 ( 新增74城市排名、2+26城市排名 )
            if (SortScopeTypeEnum.NATIONAL_SORT.getValue().equals(sortScopeType)
                || SortScopeTypeEnum.PROVINCE_SORT.getValue().equals(sortScopeType)
                || SortScopeTypeEnum.AREA_COUNTY_SORT.getValue().equals(sortScopeType)
                || SortScopeTypeEnum.OFFICE_SORT.getValue().equals(sortScopeType)
                || SortScopeTypeEnum.SEVENTY_FOUR_CITY_SORT.getValue().equals(sortScopeType)
                || SortScopeTypeEnum.TWENTY_SIX_CITY_SORT.getValue().equals(sortScopeType)) {
                iaqiList = cityAirService.findCityIAQIByRegionCodeAndTime(paraMap);
                // 办事处排名
                // 无效范围类型
            } else {
                iaqiList = new ArrayList<IAQIEntity>();
            }
            return MsgResult.okResult(iaqiList);// standardSiteMongoDataService.findAreaComprehensiveIndexData(request);
        } catch (Exception e) {
            e.printStackTrace();
            return MsgResult.exceptionResult();
        }
    }

    /**
     * 查询空气质量周报数据
     *
     * @param request
     * @param response
     * @return
     */
    @SuppressWarnings("unchecked")
    @RequestMapping(value = "findAirWeekReportData", method = {RequestMethod.GET})
    public MsgResult findAirWeekReportData(HttpServletRequest request, HttpServletResponse response) {
        // 年份
        String year = request.getParameter("year");
        if (null == year || "".equals(year)) {
            return MsgResult.infoResult("查询条件年份year为空，查询失败。");
        }
        // 第几周
        String weekNum = request.getParameter("weekNum");
        if (null == weekNum || "".equals(weekNum)) {
            return MsgResult.infoResult("查询条件周数weekNum为空，查询失败。");
        }

        // 点位数据：国控点 nationalSite;省控点 provinceSite；小型站 smallAirSite
        String pointType = request.getParameter("pointType");
        // 点位类型 默认 国控点 SiteTypeEnum.NATIONAL_SITE.getValue()
        if (null == pointType || "".equals(pointType)) {
            // return MsgResult.infoResult("查询条件点位类型pointType为空，查询失败。");
            pointType = SiteTypeEnum.NATIONAL_SITE.getValue();
        }

        // 封装request中的查询条件
        Map<String, Object> paraMap = new HashMap<String, Object>();
        paraMap.put("year", year);
        paraMap.put("weekNum", weekNum);
        paraMap.put("pointType", pointType);
        paraMap.put("pointCodes", request.getParameter("pointCodes"));

        try {

            List<IAQIEntity> weekList = null;
            // 国控点 或者 省控点 或者 小型站; 判断用户是否具有访问权限
            if ((SiteTypeEnum.NATIONAL_SITE.getValue().equals(pointType) && UserSiteUtil.userIsNotContainSiteTypes("001"))
                || (SiteTypeEnum.PROVINCE_SITE.getValue().equals(pointType) && UserSiteUtil.userIsNotContainSiteTypes("002"))
                || (SiteTypeEnum.SMALL_AIR_SITE.getValue().equals(pointType) && UserSiteUtil.userIsNotContainSiteTypes("004"))) {
                weekList = gPointAirService.findGPointWeekIAQIBySiteCodeAndTime(paraMap);
                // 无效的点位类型
            } else {
                weekList = new ArrayList<IAQIEntity>();
            }

            Map<String, Object> region = UserUtil.getRegion();
            String regionName = (String) region.get("regionName");
            Map<String, Object> returnMap = new HashMap<String, Object>();
            returnMap.put("regionName", regionName);
            returnMap.put("dataList", weekList);
            return MsgResult.okResult(returnMap);// standardSiteMongoDataService.findAirWeekReportData(request);
        } catch (Exception e) {
            e.printStackTrace();
            return MsgResult.exceptionResult();
        }
    }

    /**
     * 空气质量 同比、环比
     *
     * @param model
     * @param request
     * @return
     */
    @RequestMapping(value = "findAirQualityContrastRatio", method = RequestMethod.GET)
    public MsgResult findAirQualityContrastRatio(Model model, HttpServletRequest request) {
        // 点位数据：国控点 nationalSite;省控点 provinceSite；小型站 smallAirSite
        String pointType = request.getParameter("pointType");
        // 点位类型 默认 国控点 SiteTypeEnum.NATIONAL_SITE.getValue()
        if (null == pointType || "".equals(pointType)) {
            return MsgResult.infoResult("查询条件点位类型pointType为空，查询失败。");
        }
        // 数据类型 日报dayData;周报 weekData;月报monthData;年报yearData
        String dataType = request.getParameter("dataType");
        // 数据类型 默认 周报 DataTypeEnum.WEEK_DATA.getValue()
        if (null == dataType || "".equals(dataType)) {
            return MsgResult.infoResult("查询条件数据类型dataType为空，查询失败。");
        }

        String startTime = request.getParameter("startTime");
        String endTime = request.getParameter("endTime");
        // 查询任意时间同环比，采用日数据，参数添加startTime、endTime
        if ("dayData".equals(dataType)) {
            if (null == startTime || "".equals(startTime)) {
                return MsgResult.infoResult("查询条件开始时间startTime为空，查询失败。");
            }

            if (null == endTime || "".equals(endTime)) {
                return MsgResult.infoResult("查询条件结束时间endTime为空，查询失败。");
            }
        }
        // 年份
        String yearNum = request.getParameter("yearNum");
        if (null == yearNum || "".equals(yearNum)) {
            return MsgResult.infoResult("查询条件年份yearNum为空，查询失败。");
        }

        // 周数
        String monthOrWeekNum = request.getParameter("monthOrWeekNum");
        if (null == monthOrWeekNum || "".equals(monthOrWeekNum)) {
            return MsgResult.infoResult("查询条件月份活周数monthOrWeekNum为空，查询失败。");
        }

        // 封装request中的查询条件
        Map<String, Object> paraMap = new HashMap<String, Object>();
        paraMap.put("pointType", pointType);
        paraMap.put("dataType", dataType);
        paraMap.put("startTime", startTime);
        paraMap.put("endTime", endTime);
        paraMap.put("yearNum", yearNum);
        paraMap.put("monthOrWeekNum", monthOrWeekNum);
        paraMap.put("pointCodes", request.getParameter("pointCodes"));

        try {

            List<ContrastRatioEntity> thBList = null;
            // 国控点 或者 省控点 或者 小型站; 判断用户是否具有访问权限
            if ((SiteTypeEnum.NATIONAL_SITE.getValue().equals(pointType) && UserSiteUtil.userIsNotContainSiteTypes("001"))
                || (SiteTypeEnum.PROVINCE_SITE.getValue().equals(pointType) && UserSiteUtil.userIsNotContainSiteTypes("002"))
                || (SiteTypeEnum.SMALL_AIR_SITE.getValue().equals(pointType) && UserSiteUtil.userIsNotContainSiteTypes("004"))) {
                thBList = gPointAirService.findGPointContrastRatioBySiteCodeAndTime(paraMap);
                // 小型站
            } else {
                thBList = new ArrayList<ContrastRatioEntity>();
            }
            return MsgResult.okResult(thBList);// standardSiteMongoDataService.findAirQualityContrastRatio(request);
        } catch (Exception e) {
            e.printStackTrace();
            return MsgResult.exceptionResult();
        }
    }

    /**
     * 空气质量站点对比趋势
     *
     * @param model
     * @param request
     * @return
     */
    @RequestMapping(value = "findAirQualityContrastThrend", method = RequestMethod.GET)
    public MsgResult findAirQualityContrastThrend(Model model, HttpServletRequest request) {
        // 选中的点位编码
        String checkedSiteCodes = request.getParameter("checkedSiteCodes");
        // String nationalSiteCodes = request.getParameter("nationalSiteCodes");
        // String smallAirSiteCodes = request.getParameter("smallAirSiteCodes");
        // checkedSiteCodes = "410100004,410100051,410100052";
        if (null == checkedSiteCodes) {
            return MsgResult.infoResult("查询条件点位编码为空，查询失败。");
        }
        /*
         * if (null == smallAirSiteCodes && null== nationalSiteCodes) { return
		 * MsgResult.infoResult(
		 * "查询条件点位编码nationalSiteCodes和smallAirSiteCodes同时为空，查询失败。"); }
		 */

        // 数据类型 时报hourData；日报dayData
        String dataType = request.getParameter("dataType");
        if (null == dataType || "".equals(dataType)) {
            // return MsgResult.infoResult("查询条件数据类型dataType为空，查询失败。");
            dataType = DataTypeEnum.HOUR_DATA.getValue();
        }

        // 空气质量因子:AQI,综合指数 compositeValue,PM10,PM25,SO2,NO2,CO,O3
        String qualityFactor = request.getParameter("qualityFactor");
        if (null == qualityFactor || "".equals(qualityFactor)) {
            // return MsgResult.infoResult("查询条件空气质量因子qualityFactor为空，查询失败。");
            qualityFactor = "AQI";
        }

        // 开始时间
        String startTime = request.getParameter("startTime");
        // startTime = "2017-02-15 15:00:00";
        if (null == startTime || "".equals(startTime)) {
            return MsgResult.infoResult("查询条件开始时间startTime为空，查询失败。");
        }

        // 结束时间
        String endTime = request.getParameter("endTime");
        // endTime = "2017-02-16 12:00:00";
        if (null == endTime || "".equals(endTime)) {
            return MsgResult.infoResult("查询条件结束时间endTime为空，查询失败。");
        }

        // 封装request中的查询条件
        Map<String, Object> paraMap = new HashMap<String, Object>();
        paraMap.put("checkedSiteCodes", checkedSiteCodes);
        paraMap.put("dataType", dataType);
        paraMap.put("qualityFactor", qualityFactor);
        paraMap.put("startTime", startTime);
        paraMap.put("endTime", endTime);

        try {
            return gPointAirService.findAirQualityContrastThrend(paraMap);// standardSiteMongoDataService.findAirQualityContrastThrend(request);
        } catch (Exception e) {
            e.printStackTrace();
            return MsgResult.exceptionResult();
        }
    }

    /**
     * 根据地图级别，查询国控站点空气质量及点位信息
     *
     * @param request
     * @param response
     * @return
     */
    @RequestMapping(value = "/findNationalSiteAQIByMapLevel", method = RequestMethod.GET)
    public MsgResult findNationalSiteAQIByMapLevel(HttpServletRequest request, HttpServletResponse response) {
        try {
            String dataLevel = request.getParameter("areaLevel");
            // 区域级别，默认 全国国控站点 nationalPointLevel
            dataLevel = dataLevel == null || "".equals(dataLevel) ? "nationalSiteLevel" : dataLevel;

            // 拼凑 查询条件 Map
            Map<String, Object> paraMap = new HashMap<String, Object>();
            paraMap.put("areaLevel", dataLevel);

            Map<String, Object> cityOrSiteInfoList = cityAirService.findNationalSiteAQIByMapLevel(paraMap);
            return MsgResult.okResult(cityOrSiteInfoList);
        } catch (Exception e) {
            e.printStackTrace();
            return MsgResult.exceptionResult();
        }
    }

    /**
     * 显示当前登录用户下的国控点数据.
     *
     * @param request
     * @param response
     * @return
     */
    @RequestMapping(value = "/findGpointAQIByMapLevel", method = RequestMethod.GET)
    public MsgResult findGpointAQIByMapLevel(HttpServletRequest request, HttpServletResponse response) {
        try {


            // 拼凑 查询条件 Map
            Map<String, Object> paraMap = new HashMap<String, Object>();

            Map<String, Object> cityOrSiteInfoList = cityAirService.findGpointAQIByMapLevel(paraMap);


            return MsgResult.okResult(cityOrSiteInfoList);
        } catch (Exception e) {
            e.printStackTrace();
            return MsgResult.exceptionResult();
        }
    }

    /**
     * 查询点位及点位详细信息
     *
     * @param request
     * @param response
     * @return
     */
    @RequestMapping(value = "/findSiteAndDetailInfoBySiteTye", method = RequestMethod.GET)
    public MsgResult findSiteAndDetailInfoBySiteTye(HttpServletRequest request, HttpServletResponse response) {
        String pointType = request.getParameter("pointType");
        // 点位类型 默认 国控点
        pointType = null == pointType || "".equals(pointType) ? "allAirSite" : pointType;
        Map<String, Object> paraMap = new HashMap<String, Object>();
        paraMap.put("pointType", pointType);

        try {
            Map<String, Object> siteInfoList = cityAirService.findSiteAndDetailInfoBySiteType(paraMap);
            return MsgResult.okResult(siteInfoList);
        } catch (Exception e) {
            e.printStackTrace();
            return MsgResult.exceptionResult();
        }
    }

    /**
     * 根据点位编码，查询单个点位详细信息 比如，空气站最近两个小时的5分钟数据等
     *
     * @param request
     * @param response
     * @return
     */
    @RequestMapping(value = "/findSiteLastDetailBySiteCode", method = RequestMethod.GET)
    public MsgResult findSiteLastDetailBySiteCodeForTrend(HttpServletRequest request, HttpServletResponse response) {
        // 点位编码
        String siteCode = request.getParameter("siteCode");
        // siteCode = "125";
        // 点位类型
        String siteType = request.getParameter("siteType");
        // 默认国控点nationalSite（省控点 provinceSite 小型站 smallAirSite 工业源 industrySite）
        siteType = null == siteType || "".equals(siteType) ? "nationalSite" : siteType;

        if (null == siteCode || "".equals(siteCode)) {
            return MsgResult.infoResult("查询条件点位编码siteCode为空，查询失败。");
        }

        Map<String, Object> paraMap = new HashMap<String, Object>();
        paraMap.put("siteType", siteType);
        paraMap.put("siteCode", siteCode);

        try {
            Map<String, Object> datailList = cityAirService.findAirSiteLastDetailListBySiteTypeForTrend(paraMap);
            return MsgResult.okResult(datailList);
        } catch (Exception e) {
            e.printStackTrace();
            return MsgResult.exceptionResult();
        }

    }

    /**
     * 根据区域编码，获取区域的最新空气质量数据 如果区域编码为空，则为登录用户所属区域的空气质量数据
     *
     * @param request
     * @param response
     * @return
     */
    @RequestMapping(value = "/findCityLiveAQIByRegionCode", method = RequestMethod.GET)
    public MsgResult findCityLiveAQIByRegionCode(HttpServletRequest request, HttpServletResponse response) {

        // 需要查询的区域的区域编码
        String regionCode = request.getParameter("regionCode");

        try {

            Map<String, Object> cityLiveAQI = cityAirService.findCityLiveAQIByRegionCode(regionCode);

            return MsgResult.okResult(cityLiveAQI);
        } catch (Exception e) {
            e.printStackTrace();
            return MsgResult.exceptionResult();
        }
    }

    /**
     * 获取当日AQI小时累计，用于全局总揽展示 需要显示每个小时的AQI累计
     *
     * @param request
     * @param response
     * @return
     */
    @RequestMapping(value = "/findCityCurrentDayAQITotalForQJZL", method = RequestMethod.GET)
    public MsgResult findCityCurrentDayAQITotalForQJZL(HttpServletRequest request, HttpServletResponse response) {
        try {
            JSONArray aqiTotalArr = cityAirService.findUserCityCurrentDayAQITotal();
            return MsgResult.okResult(aqiTotalArr);
        } catch (Exception e) {
            e.printStackTrace();
            return MsgResult.exceptionResult();
        }

    }

    /**
     * 获取城市所属区域郊县的综合指数当日累计数据
     *
     * @param request
     * @param response
     * @return
     */
    @RequestMapping(value = "/findCityCurrentComprehensiveIndexTotalForQJZL", method = RequestMethod.GET)
    public MsgResult findCityCurrentComprehensiveIndexTotalForQJZL(HttpServletRequest request,
                                                                   HttpServletResponse response) {
        try {
            Map<String, Object> cityCIMap = cityAirService.findCityCurrentComprehensiveIndexTotalForQJZL();
            return MsgResult.okResult(cityCIMap);
        } catch (Exception e) {
            e.printStackTrace();
            return MsgResult.exceptionResult();
        }
    }


    /**
     * 查询当前登录用户的排名情况
     *
     * @param request
     * @return
     * @author 李亚坤
     * @createtime 2017年3月30日13:21:52
     */
    @RequestMapping(value = "findCityRankings", method = RequestMethod.GET)
    public MsgResult findCityRankings(HttpServletRequest request) {
        try {
            Object resultObj = cityAirService.findCityRankings();
            return MsgResult.okResult(resultObj);
        } catch (Exception e) {
            e.printStackTrace();
            return MsgResult.exceptionResult();
        }
    }

    /**
     * 查询当前登录用户的74City排名情况
     *
     * @param request
     * @return
     * @author 毛超然
     * @createtime 2017年8月4日09:21:52
     */
    @RequestMapping(value = "find74CityRanking", method = RequestMethod.GET)
    public MsgResult find74CityRanking(HttpServletRequest request) {
        String order = request.getParameter("order");
        if (null == order || "".equals(order)) {
            order = "positive";
        }
        Map<String, Object> paramMap = new HashMap<>();
        paramMap.put("order", order);
        try {
            Object resultObj = cityAirService.find74CityRanking(paramMap);
            return MsgResult.okResult(resultObj);
        } catch (Exception e) {
            e.printStackTrace();
            return MsgResult.exceptionResult();
        }
    }


    /**
     * 点位 日报 导出excel 方法
     *
     * @param request
     * @param response
     */
    @RequestMapping(value = "dayReportExportExcel", method = {RequestMethod.GET, RequestMethod.POST})
    public void dayReportExportExcel(HttpServletRequest request, HttpServletResponse response) {
        Workbook wb;
        try {
            // 点位数据：国控点 nationalSite;省控点 provinceSite；小型站 smallAirSite
            String pointType = request.getParameter("pointType");
            // 点位类型 默认 国控点 SiteTypeEnum.NATIONAL_SITE.getValue()
            if (null == pointType || "".equals(pointType)) {
                // return MsgResult.infoResult("点位类型pointType的值为空，查询失败。");
                pointType = SiteTypeEnum.NATIONAL_SITE.getValue();
            }
            Calendar calendar = Calendar.getInstance();
            calendar.add(Calendar.DAY_OF_MONTH, -1);
            // 开始时间
            String startTime = request.getParameter("startTime");
            startTime = null == startTime || "".equals(startTime)
                ? DateTimeUtil.getFormatDateStr(calendar.getTime(), DamConstant.ALL_SDF_YMD) : startTime;
            // 结束时间
            String endTime = request.getParameter("endTime");
            calendar.add(Calendar.DAY_OF_MONTH, 1);
            endTime = null == endTime || "".equals(endTime)
                ? DateTimeUtil.getFormatDateStr(calendar.getTime(), DamConstant.ALL_SDF_YMD) : endTime;

            // 查询条件
            Map<String, Object> paraMap = new HashMap<String, Object>();
            paraMap.put("pointType", pointType);
            paraMap.put("startTime", startTime);
            paraMap.put("endTime", endTime);
            // 数据类型，查询日数据
            paraMap.put("dataType", "dayData");
            // 数据计算类型，实时数据
            paraMap.put("dataCalculateType", "realTimeData");

            List<AQIEntity> dayAirQualityList = gPointAirService.findAirSiteAQIByTime(paraMap);

            wb = ExcelUtil.fillDayReportData2ExcelWithTemplate(dayAirQualityList, "dayReportTemplate.xlsx", "空气质量日报" + startTime);

            ResponseUtil.export(response, wb, "空气质量日报" + startTime + ".xlsx");

        } catch (Exception e) {
            e.printStackTrace();
        }

    }

    /**
     * 查询热力图数据,全国城市数据
     *
     * @param request
     * @return
     */
    @RequestMapping(value = "findHotPointData", method = RequestMethod.GET)
    public MsgResult findHotPointData(HttpServletRequest request) {
        try {
            String searchTime = request.getParameter("searchTime");
            String siteTypeCode = request.getParameter("siteTypeCode");
            // 根据登录用户，查询用户所属行政区域
            Map<String, Object> region = UserUtil.getRegion();
            String regionCode = (String) region.get("regionCode");
            if (StringUtils.isEmpty(searchTime)) {
                Calendar calendar = Calendar.getInstance();
                calendar.add(Calendar.HOUR, -1);
                searchTime = CalendarUtil.getDateFormat(calendar.getTime(), DamConstant.ALL_SDF_YMD_H);
            }

            Map<String, Object> paramMap = new HashMap<>();
            paramMap.put("searchTime", searchTime);
            paramMap.put("regionCode", regionCode);
            paramMap.put("siteTypeCode", siteTypeCode == null ? "001" : siteTypeCode);
            paramMap.put("siteType", SiteTypeEnum.ALL_AIR_SITE.getValue());
            Map<String, Object> resultMap = null;
            resultMap = cityAirService.findHotPointData(paramMap);


            return MsgResult.okResult(resultMap);
        } catch (Exception e) {
            e.printStackTrace();
            return MsgResult.exceptionResult();
        }
    }

    /**
     * 查询小型站热力图数据
     *
     * @param request
     * @return
     */
    @RequestMapping(value = "findHotPointDataWithSmallAir", method = RequestMethod.GET)
    public MsgResult findHotPointDataWithSmallAir(HttpServletRequest request) {
        try {
            String searchDate = request.getParameter("searchDate");
            String startTime;
            String endTime;
            Map<String, Object> paramMap = new HashMap<>();
            Calendar calendar = Calendar.getInstance();
            if (StringUtils.isEmpty(searchDate)) {
                endTime = CalendarUtil.getDateFormat(calendar.getTime(), DamConstant.SDF_YMD_HMS);
            } else {
                endTime = searchDate;
                Date endDate = CalendarUtil.getDateByString(searchDate, DamConstant.SDF_YMD_HMS);
                calendar.setTime(endDate);
            }
            calendar.add(Calendar.MINUTE, -10);
            startTime = CalendarUtil.getDateFormat(calendar.getTime(), DamConstant.SDF_YMD_HMS);
            paramMap.put("endTime", endTime);
            paramMap.put("startTime", startTime);
            // 获取登录用户所属区域信息
            Map<String, Object> region = UserUtil.getRegion();
            // 用户所属区域
            String userRegionCode = (String) region.get("regionCode");
            paramMap.put("regionCode", userRegionCode);
            Map<String, Object> resultMap = smallAirService.findHotPointDataWithSmallAir(paramMap);
            return MsgResult.okResult(resultMap);
        } catch (Exception e) {
            e.printStackTrace();
            return MsgResult.exceptionResult();
        }

    }

}
